const LApp = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ProductModel: null,
    isEdit: '',
    areaIndex: 0,
    verIndex: 0,
    productSelectItem: [],
    VersionList: [{ Code: 0, Name: '标准' }, { Code: 1, Name: '黄金' }, { Code: 2, Name: '铂金' }, { Code: 3, Name: '钻石' }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isEdit === "false") {
      wx.setNavigationBarTitle({
        title: '产品详情',
      })
    }
    let model = LApp.pages.get("productBatch/productBatch").ProductModel;
    if (model) {
      model.VersionName = this.data.VersionList[model.Version].Name;
      this.setData({
        ProductModel: model,
        isEdit: options.isEdit
      })
    } else {
      this.setDate({
        ProductModel: {}
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getProductSelectItemList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getProductSelectItemList: function () {
    let _this = this;
    if (_this.data.isEdit === "false") {
      return;
    }
    LApp._get({
      url: "nbapi/product/selectitem",
      data: { enterpriseId: LApp.globalData.userInfo.EnterpriseId, typeId: this.data.ProductModel.ProductTypeId },
      success: function (r) {
        _this.setData({
          productSelectItem: r
        })
        for (var item in _this.data.productSelectItem) {
          if (_this.data.productSelectItem[item].Code === _this.data.ProductModel.ProductId) {
            _this.setData({
              areaIndex: item
            })
            break;
          }
        }
      }
    })
  },
  productNameSelect: function (e) {
    this.setData({
      areaIndex: e.detail.value
    });

    this.data.ProductModel.ProductName = this.data.productSelectItem[this.data.areaIndex].Name;
    this.data.ProductModel.ProductId = this.data.productSelectItem[this.data.areaIndex].Code;
    this.setData(this.data)
  },
  VersionSelect:function(e){
    this.data.verIndex = e.detail.value;
    var _version = this.data.VersionList[this.data.verIndex];
    if(_version){
      this.data.ProductModel.VersionName = _version.Name;
      this.data.ProductModel.Version = _version.Code;
    }
    this.setData(this.data);
  },
  onSaveClick: function (e) {
    LApp._post({
      url: "nbapi/productbatch/update",
      data: this.data.ProductModel,
      success: function (r) {
        wx.showToast({
          title: "修改完成",
          icon: "success",
          showCancel: false,
          success: function () {
            wx.$$getParent().data.productBatchList = [];
            wx.$$getParent().getProductBatchList();
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    })
  },
  bindModelChange: function (e) {
    wx.$$apply(e, this.data.ProductModel);
  },
  pageComeBack: function () {
    wx.navigateBack({
      delta: 1
    })
  }
})